//
//  AppDataManager.h
//  BanBu
//
//  Created by 17xy on 12-8-3.
//
//

#import <Foundation/Foundation.h>
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMDatabaseQueue.h"
#import "UIIImage_TKCategory.h"
#import "BanBu_MyFriendViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "TKLoadingView.h"
#import "BanBu_ChatCellAPI.h"
#import "BanBu_EmoKeyboard.h"
#import "BanBu_ManuscriptAPI.h"
#import "BanBu_MagicWordsAPI.h"

@class BanBu_ChatViewController;
@class AppCommunicationManager;
@class BanBu_AppDelegate;
@class BanBu_MyFriendViewController;

@class BanBu_TransmitViewController;
@class BanBu_FriendListController;
@class BanBu_SecondaryDialogueViewController;
@class BanBu_SmileView;
@class BanBu_NoDisturbingViewController;
@class BanBu_EmoStoreViewController;
@class BanBu_ManuscriptViewController;

#define DataCachePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:@"Userdata"]
#define MyAppDataManager [AppDataManager sharedAppDataManager]
#define FileManager      [NSFileManager defaultManager]
#define UserDefaults     [NSUserDefaults standardUserDefaults]
#define lastHostIP @"lastBanbuHostIP"
#define  DIALOGS @"dialogs"
#define  TALKPEOPLES [@"talkPeoples" stringByAppendingString:MyAppDataManager.useruid]
#define  BallDialogs @"balldialogs"
#define  BallList [@"balllist" stringByAppendingString:MyAppDataManager.useruid]

#define  TALKPEOPLESTABLE [@"chatTable" stringByAppendingString:MyAppDataManager.useruid]

//建一张表
#define  AgreeList [@"agreelist"stringByAppendingString:MyAppDataManager.useruid]
#define PersonalDataList [@"personalData"stringByAppendingString:MyAppDataManager.useruid]
#define UserBaseDataList [@"userBaseData"stringByAppendingString:MyAppDataManager.useruid]
#define DefaultReadNum 20
#define AllData 1000000

#define VALUE(aKey,aDic)  [aDic valueForKey:aKey]
#define AsynRun(selector)  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_async(queue, ^{selector;})



#define BanBu_release(_v)  [_v release],_v=nil


typedef void (^MyDataManagerArray)(NSArray*);

typedef enum{
MessageTypeStand,

MessageTypeRadio

}MessageType;

@protocol AppDataManagerDelegate;

@interface AppDataManager : NSObject<AVAudioPlayerDelegate>
{
      AVAudioPlayer *player;
}

//版本号2014/07/04添加
@property (nonatomic, copy) NSString * appVersion;

@property(nonatomic, copy)NSString *SelectGroupName;

@property(nonatomic,assign) NSString* hidecell;
@property(nonatomic)BOOL     locationServicesEnabled;
@property(nonatomic, retain) NSMutableString *banbuHostIP;
@property(nonatomic, retain)NSString *musicName;
@property(nonatomic, retain)NSString *sendMsgMusicName;//发消息

@property(nonatomic, retain)NSMutableDictionary *regDic;
@property(nonatomic, retain)NSMutableDictionary *cellRowMapDic;//这个字典是为了chatid与cell的row映射起来
@property(nonatomic, retain)NSString *loginid;
@property(nonatomic, retain)NSString *useruid;
@property(nonatomic, retain)NSString *chatuid;      //对话界面会话对方的id
@property(nonatomic, retain)NSString *userAvatar;
@property(nonatomic, retain)NSMutableArray *nearBuddys;
@property(nonatomic, retain)NSMutableArray *nearDos;
@property(nonatomic, retain)NSMutableArray *contentArr;
@property(nonatomic, retain)NSMutableArray *friends;
@property(nonatomic, retain)NSMutableArray *friendsDos;
@property(nonatomic, retain)NSMutableArray *friendDos;
@property(nonatomic, retain)NSMutableArray *friendViewList;
@property(nonatomic, retain)NSMutableArray *dialogs;
@property(nonatomic, retain)NSMutableArray *talkPeoples;
@property(nonatomic, retain)NSMutableArray *unLoginArr;

@property(nonatomic, retain)NSString *zibarString;
@property(nonatomic, retain)FMDatabase *dataBase;
@property(nonatomic, assign)BanBu_ChatViewController *appChatController;
@property(nonatomic, assign)BanBu_SecondaryDialogueViewController * secondaryDialog;
@property(nonatomic, assign)BanBu_NoDisturbingViewController * noDisturbingControlle;
@property(nonatomic, assign)BanBu_EmoStoreViewController * emoStoreController;
@property(nonatomic, assign)BanBu_ManuscriptViewController * manuscriptController;
//yinlin 2014年08月07日13:19:50,认证返回的界面
@property(nonatomic, assign) UIViewController *personCon;

@property(retain, nonatomic)NSMutableArray * secondaryTalkPeople; //二级目录的数据源，一般存放打招呼的数据源

// 这是实现代理的
// 这是执行的判断是否深查询

@property(nonatomic,assign)BOOL deep;
@property(nonatomic,assign)NSString *Lasttime;

// 这是不文明用语
@property(nonatomic,retain)NSString *LanguageDateString;
@property(nonatomic,retain)NSString *dateString;
@property(nonatomic,retain)NSString *blackString;
@property(nonatomic,retain)NSMutableDictionary *languageDictionary;

@property(nonatomic,assign)NSInteger k;//控制

// 这是控制是不是对讲机模式
@property(nonatomic,assign)MessageType Messagetype;
@property(nonatomic,retain)NSMutableDictionary *messageRadioDictionary;
@property(nonatomic,retain)NSMutableArray *keyArr;
@property(nonatomic,retain)NSMutableArray *valueArr;

// 发过来的多语言

@property(nonatomic,retain)NSMutableArray *emiNameArr;
@property(nonatomic,retain)NSMutableArray *emiLanguageArr;

@property(nonatomic,assign)BOOL isSee;

// 这是同意申请的列表

@property(nonatomic,retain)NSMutableArray *agreeList;

// 这是搞收到消息的时间的
@property(nonatomic,assign)float time;
@property(nonatomic,retain)NSMutableArray *talkArr;

@property(nonatomic,assign)BOOL isPlay;

@property(nonatomic,retain)AVAudioPlayer *playert;

@property(nonatomic,retain)NSMutableArray *boolArr;
//已经读过用户资料的数目，每五次提交一次
@property(nonatomic,retain)NSMutableDictionary *peopleReaded;
//广播未读消息个数信息
@property(nonatomic,retain)NSMutableDictionary *unReadBroadInfor;
+ (AppDataManager *)sharedAppDataManager;

-(void)setMessagetype:(MessageType)Messagetype;

//文件缓存路径
- (UIImage *)imageForImageUrlStr:(NSString *)fileUrlStr;
- (NSData *)imageDataForImageUrlStr:(NSString *)fileUrlStr;
-(NSData *)msgimageDataForImageUrlStr:(NSString *)fileUrlStr;
- (UIImage *)msgImageForImageUrlStr:(NSString *)fileUrlStr;
-(NSData *)imageDataForImageUrlStr:(NSString *)fileUrlStr withFolder:(NSString *)folerName;

//用于查找本地图片
-(NSData *)imageDataForImagePathStr:(NSString *)imagePath;

-(NSString *)getPar1StrFromDic:(NSDictionary *)parDic;

-(NSString *)IsMinGanWord:(NSString *)input;
//检查是否是彩蛋，YES：是彩蛋，并且能播放；NO：不是彩蛋或是彩蛋但不能播放
-(NSArray *)easteregg:(NSString *)text;

-(NSString *)IsInternationalLanguage:(NSString *)input;
-(NSString *)theRevisedName:(NSString*)oldname andUID:(NSString *)uid;

-(NSString *)currentTime:(NSString *)receiveTime;

-(NSString*)currentTimeBeforeAweek;

-(NSString*)getPreferredLanguage;


#pragma mark - 获取对话消息数据,界面显示
// 加载更多
-(BOOL)readMoreTwentyMessage;

//读取指定数目的消息
- (void)readTalkList:(NSString *)touid WithNumber:(NSInteger)num;

//查询数据库中对话表信息或加载聊天信息表信息，返回查询结果
- (NSMutableArray *)readTalkListAfterSendImage:(NSString *)touid WithNumber:(NSInteger)num;

//获取userid下的用户，现在对话表分为两级，level：0或空为顶级，直接在对话中显示；400、600等为顶级，点击顶级以后进入二级页面显示二级的用户，如好友验证在600下面。系统通知在400下，系统警告在500下；
-(NSArray *)readTalkPeopleByUserID:(NSString *)userid;

/*************************************/
#pragma mark 发送消息
//发送消息
-(void)sendMsg:(NSDictionary *)aMsg toUid:(NSString *)uid;
-(void)sendReadedStatus:(NSArray *)useridArray;

#pragma mark - 全局接受消息（非点对点模式）
-(void)writeToDB:(NSDictionary *)people;

@property (assign, nonatomic)BOOL newRecord;//有新的来访记录
#pragma mark - 对话表的处理
-(void)initTalkPeopleDB;
-(BOOL)initTalkPeopleOne:(NSDictionary *)people withEffectiveNum:(NSInteger)effectiveNum;
-(void)writeToTalkPeopleOne:(NSDictionary *)dataDic withEffectiveNum:(NSInteger)effectiveNum;
-(void)writeToTalkPeopleOne:(NSDictionary *)dataDic withEffectiveNum:(NSInteger)effectiveNum withLevel:(NSString *)level;

-(void)setUnreadNumber:(NSInteger)number With:(NSString *)userid;
-(void)setTalkPeopleOne:(NSDictionary *)updataDic;

//addBy:Wang Xj at 2013-7-3 15:01
//获取全局未读消息数量
-(NSInteger)getBadgeValue;

-(NSInteger)getAllBadgeValue;

//查看进入聊天界面的对方是否存在未读的消息，返回未读消息的条数
-(NSInteger)clearUnReadDialogsNumber:(NSString *)chatUID;

//删除对话列表   修改时间：2013-8-19：17：07：00
-(void)deleteTalkPeople:(NSArray *)useridArray;
//将拉入黑名单的人的对话删除
-(void)deleteBlackDialog:(NSString *)userID;

//更新对话表中用户的level
-(void)updateDialogUserLevel:(NSString *)userid toLevel:(NSString *)level;

//获取未读消息数量和打招呼的人的数量（一个是消息数量、一个是人的数量）
-(NSDictionary * )getUnreadNumBySayHi:(NSString *)userid;

//更新打招呼（600号）的content、stime、type、unReadNum、me、status为最后一条打招呼的内容
-(void)updateLastDialogBySayHi:(NSDictionary *)amsg;

//更新未读消息
-(void)updateUnreadNum;

//对话表创建系统用户：600
-(BOOL)initSystemUser:(NSString *)userid ByMsg:(NSDictionary *)amsg;

//更新打招呼的未读消息条数
-(void)updateSayHiUnReadNum;

#pragma mark - 聊天详情表的处理
-(NSString *)getTableNameForUid:(NSString *)uid;

-(NSString *)searchMaxChatid:(NSString *)chatuid;

//初始化十张聊天记录表，表名chat_myID_0(0~9)
-(BOOL)initDetailChatMessageTable;

-(void)setStatusOfOne:(NSDictionary *)statuDic;
-(void)setStatusOfAll:(NSArray *)statusList;

-(NSInteger)writeToDialogDB:(NSDictionary *)people;

//写入聊天记录表中聊天信息
-(void)writeToDetailChatTable:(NSDictionary *)aMsg andToUid:(NSString *)toUID;

//写聊天记录表,更新聊天记录表中该消息的发送状态发“发送中”，查找对话表中该用户的最后一条信息是否为本次发送的信息，如果是则状态改为发送中
-(void)updateDialogStateByReSend:(NSDictionary *)statuDic;

//当对话界面的消息已读状态与聊天信息最后一条信息的已读状态不一致时要更新对话表（注：该消息必须是自己发的）
-(void)updateStstus:(NSString *)userID number:(NSNumber *)number;

-(void)updateStime:(NSDictionary *)amsg;

-(BOOL)setMediaStatusReaded:(NSString *)chatid userID:(NSString *)userid;//设置声音消息状态为已读

//删除一个人所有的聊天记录，不删除该人（即在对话表中有该人的记录，在聊天记录表中没有该人的记录）
-(BOOL)deleteAllChattingRecordsOfOnePerson:(NSString *)userID;

//删除一个人一条聊天记录
-(BOOL)deleteOneChattingRecordOfOnePerson:(NSDictionary *)amsg;

//阅后即焚消息在删除时，同时清除对话表的数据
-(void)deleteTalkRecordOfOnePerson:(NSDictionary *)amsg;

//将开放平台发送的消息的跳转保存到聊天详情表
-(void)updateDialogLinkTo:(NSString*)linkto toUserID:(NSString *)userid toChatID:(NSString *)chatid;

//清空所有二级目录；userid为二级目录的level值如：600
-(void)clearAllSecondaryUserMsg:(NSString *)userid andUserArray:(NSArray *)secondaryUser;
//更新打招呼（600号）的content、stime、type、unReadNum、me、status为最后一条打招呼的内容
-(void)setLastDialogBySayHi:(NSDictionary *)amsg;

//更新阅后即焚消息的有效时间
-(BOOL)setEffectiveTime:(NSDictionary *)amsg;

-(void)setHeightSp:(NSDictionary *)amsg;

#pragma mark - 个人资料表的处理

-(void)writePersonalDataToDB:(NSArray *)personalDataList;

-(NSMutableArray *)getPersonalDataIn:(NSString *)_minute;

-(NSArray *)getPersonalDataOfUserid:(NSArray *)_users;
-(NSTimeInterval )getTheSecondLastOnlineTime:(NSString *)_lastOnlineTime;//距离最后上线时间的秒数，还有其他的时间计算也使用
-(void)cleanUserOldBaseData;//清除七天前的数据，只针对KeyLastSeeDynamicTime
-(NSString *)getTheLastOnlineTime :(NSString *)_lastOnlineTime;//返回显示的多久前上线
-(NSString *)getServerTime;//获取服务器时间;
-(NSString *)getLanguageSubmitToServer;
-(NSString *)getLanguageSubmitToGoogle;
//用户基本杂乱信息表处理
- (NSDictionary *)getUserBaseDataWithKey:(NSString *)_key;
- (void)modifyUserBaseDataWithKey:(NSString *)_key andData:(NSDictionary *)dataDic;
-(void)deleteUserBaseDataWithKey:(NSString *)_key;
//更新上线时间
-(void)updateLastOnlineTimeAndDistanceOfPersonalDB:(NSArray *)_updateInfo;

//conditionDic存放清理条件：超过一定时间、或其他
-(void)clearPersonDB:(NSDictionary *)conditionDic;

//systemUserid:打招呼为600
-(NSDictionary *)getTheLastSayHiUserinfo:(NSString *)systemUserid;

@property (retain, nonatomic) NSMutableDictionary * lastSayHiUserInfoDic;//打招呼最后一条消息的人的个人资料

//获取单个人的个人资料
-(NSDictionary *)getPersonInfoByOne:(NSString *)userid;

#pragma mark -
@property NSTimeInterval timeDifference;//本地时间与服务器时间的差

//addBy: Wang Xj at 2013-7-15 09:21:00
@property (retain, nonatomic)NSMutableDictionary * userDialogsDic;   //存放用户最后一条消息

@property (assign, nonatomic)NSInteger selectedBgIndex;   //聊天界面选中显示背景图片的索引,在背景设置时聊天界面显示时有用
@property (assign, nonatomic)NSInteger selectedBubbleIndex;//聊天界面选中显示的气泡的索引,在气泡选择时有用

@property (retain, nonatomic)NSMutableDictionary * bubbleNameDic;

//@property (retain, nonatomic)UIImageView * imageView;

@property (assign, nonatomic)NSInteger totalLoadedPeopleNum;

@property (assign, nonatomic) BanBu_TransmitViewController * transmitController;
@property (assign, nonatomic) BanBu_FriendListController * friendController;

@property (retain, nonatomic) BanBu_SmileView * smileViewBackup;//发消息时的表情键盘
@property (retain, nonatomic) BanBu_EmoKeyboard * emoKeyboard;//表请键盘

@property (retain, nonatomic)NSMutableString *newVersionNumber;
@property (assign, nonatomic)NSInteger newVersionNumberInt; //版本号

//addat:2013-7-23 10:29:00
@property (retain, nonatomic)NSMutableArray * readedUserArray;  //存储快速查看很多人消息时往服务器发送的已读状态列表的人的id；

@property (retain, nonatomic)NSString * serverTime;//存服务器时间

@property (retain, nonatomic)NSDate * lastShowwingTime;//在取聊天记录时，先获取消息记录的时间，然后跟lastShowwingTime比较，两者时间差大于3m了，就显示显示，否则不显示时间

#pragma mark-
//判断好友关系，YES：好友；NO：非好友
-(BOOL)judgeRelationship:(NSString *)userid;

-(NSDate *)getLastShowTime:(NSString *)userid;//获取最后一次显示时间

//2013-8-13 13：00：00 addBy：Wang Xj,level是需要屏蔽的用户
-(NSArray *)getUserID:(NSString *)level;

-(NSString *)getDmeter:(NSString *)userid;

//设置信息的高度、主要是图片信息
-(double)setHeight:(NSData *)imageData;

-(double)setReceiveImageHeight:(NSDictionary *)resDic;

//保存转发的用户的消息，转发给最近对话
-(NSDictionary *)saveTransmitMsg:(NSDictionary *)msgDic andUserIDList:(NSDictionary *)userIDDic;

//保存转发的用户的消息，转发给好友
-(NSDictionary *)saveTransmitMsg:(NSDictionary *)msgDic andUseruserInfoDic:(NSDictionary *)userInfoDic;

- (UIImage *)scaleImage:(UIImage *)image proportion:(float)scale;

-(NSArray *)getPersonInfo:(NSArray *)useridList;
//显示金币
-(void)showGoldView:(NSNumber *)number andView:(UIView *)_aview;
@property (assign, nonatomic) BOOL recordingVideo; //录制视频中

//已聊过天表
-(BOOL)initChatYetTable;

//从对话表中获取个人资料
-(NSArray *)getPersonalDataOfUseridFromTalk:(NSArray *)_users;
-(NSDictionary *)getPnameArray:(NSArray *)userList;

//2014年11月04日15:14:10 初始化
-(void)setSmileView;

@end

@protocol AppDataManagerDelegate <NSObject>

@optional

- (void)appDataManagerDidUpdateDialogs;

@end












